package de.gwtprime.server.sql;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import de.gwtprime.server.model.ArticleSection;


/**
 * Use a singelton to get the SessionFactory
 * @author jan
 *
 */
public enum HibernateEntityManagerSingeltonNew {

	INSTANCE;

	private static SessionFactory sessionFactory = configureSessionFactory();
	private static ServiceRegistry serviceRegistry;

	private static SessionFactory configureSessionFactory() throws HibernateException {
		try{
			Configuration configuration = new Configuration();
			configuration.addAnnotatedClass(ArticleSection.class);
			configuration.configure("hibernateStaging.cfg.xml");
			ServiceRegistryBuilder  ssrb = new ServiceRegistryBuilder();

			ssrb.applySettings(configuration.getProperties());
			serviceRegistry = ssrb.buildServiceRegistry();

			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		}catch(Exception e){
			throw e;
		}
		return sessionFactory;
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}
